﻿using ConsumablesCabinet;
using IntelligentPharmacy.Entity;
using IntelligentPharmacy.httprequest;
using IntelligentPharmacy.publicclass;
using SmartForcer.Enum;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;

namespace SmartForcer
{
    /// <summary>
    /// 主仓人员操作界面
    /// </summary>
    public partial class InDetailWindow : Window
    {
        public MessageDialogResult dialogResult = MessageDialogResult.Yes;

        requestService request = new requestService();
        /// <summary>
        /// 标签数量
        /// </summary>
        List<TagEntity> lists = null;
        /// <summary>
        ///出库数据
        /// </summary>
        List<MaterialnformationEntity> outLists = new List<MaterialnformationEntity>();
        List<MaterialnformationEntity> putLists = new List<MaterialnformationEntity>();
        List<MaterialnformationEntity> allList = new List<MaterialnformationEntity>();
        Dictionary<string, string> tpc = new Dictionary<string, string>();
        List<TagEntity> tagOutList = new List<TagEntity>();
        List<TagEntity> tagPutList = new List<TagEntity>();
        public InDetailWindow(List<TagEntity> outlist,List<TagEntity> putlist,Dictionary<string,string> tpc)
        {
            try
            {
                this.tpc = tpc;
                this.tagOutList = outlist;
                this.tagPutList = putlist;
                if (outlist.Count > 0 || putlist.Count > 0)
                {
                    outLists = request.getRfidContent(outlist);
                    putLists = request.getRfidContent(putlist);
                }
                InitializeComponent();
            }catch(Exception e)
            {

            }
        }


        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            try
            {
                if (tpc != null)
                {
                    bqsl.Content = tpc.Count;
                }
                txt_CKKF.Text = ReadAppConfig.readerWarehouseName();
                userName.Content = Global.User.empName;
                foreach (MaterialnformationEntity entity in outLists)
                {
                    entity.putOrout = "领用";
                    allList.Add(entity);
                }
                foreach (MaterialnformationEntity entity in putLists)
                {
                    entity.putOrout = "归还";
                    allList.Add(entity);
                }
                lvw_RK.ItemsSource = allList;
                lbl_Totl.Content = allList.Count;
            }catch(Exception ex) { }
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Exit_Click(object sender, RoutedEventArgs e)
        {          
            this.Close();
        }


        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private bool isCheck=true;
        private void btnC_Submit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (isCheck)
                {
                    isCheck = false;
                    Submit();
                    Thread.Sleep(1000);
                    isCheck = true;
                }
            }catch(Exception ex)
            {

            }
        }
        bool ck=false, rk=false;
        void Submit()
        {
            bool ck = true;
            bool rk = true;
            if (tagOutList!=null && tagOutList.Count>0)
            {
                ck = request.outPutCabinet(tagOutList, 0 + "");//出库
                if (!ck)
                {
                    Global.MessageWindow.ShowDialog("出库提交失败");
                }
            }
            if (tagPutList!=null && tagPutList.Count>0)
            {
                rk = request.outPutCabinet(tagPutList, 1 + "");//入库
                if (!rk)
                {
                    Global.MessageWindow.ShowDialog("入库提交失败");
                }
            }
            if (ck || rk)
            {
                Global.MessageWindow.ShowDialog("提交成功");
                foreach (Window win in Global.WindowsList)
                    win.Close();
            }
        }

        /// <summary>
        /// 重新盘点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Clear_Click(object sender, RoutedEventArgs e)
        {
            dialogResult = MessageDialogResult.Cancel;
            this.Close();
        }


        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
            }
            catch(Exception ex) 
            { }
        }

        private void Complete_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
           
        }

     
      

        private void txt_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
            }
        }

     
        private void btnC_Submit_TouchDown(object sender, System.Windows.Input.TouchEventArgs e)
        {

           // Submit();
        }
    }
}
